/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.sany.cterminal.schedeled;

import cn.sany.cterminal.common.constants.CterminalConst;
import cn.sany.cterminal.dao.*;
import cn.sany.cterminal.entity.RealtimeInfo;
import cn.sany.cterminal.entity.StatisticHistory;
import cn.sany.cterminal.entity.VehicleInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @Project : sany
 * @Package : cn.sany.cterminal.schedeled
 * @Class : StatisticTask
 * @Description : 数据统计任务,每天23.35执行
 * @author : zhouxing
 * @CreateDate : 2020-09-17 22:54:05
 * @version : V0.5
 * @Copyright : 2020 sany.com Inc. All rights reserved.
 * @Reviewed :
 * @UpateLog :    Name    Date    Reason/Contents
 *             ---------------------------------------
 *                ****    ****    ****
 *
 */
@Slf4j
@Component
public class StatisticTask {
    @Autowired(required = false)
    VehicleInfoDao vehicleInfoDao;
    @Autowired(required = false)
    StatisticHistoryDao statisticHistoryDao;
    @Autowired(required = false)
    RealtimeInfoDao realtimeInfoDao;
    /**
     * 每天23.35执行
     */
    @Scheduled(cron = "0 55 23 * * ?")
    public void statisticData() throws Exception {
        log.info("数据统计任务开始...");
        List<VehicleInfo> vehicleInfos = vehicleInfoDao.selectVehicleByEnableState(CterminalConst.EnableState.ABLE);
        List<RealtimeInfo> realtimeInfos = realtimeInfoDao.selectAll();
        Map<Integer, RealtimeInfo> realtimeInfoMap = realtimeInfos.stream().collect(Collectors.toMap(RealtimeInfo::getVehicleId, RealtimeInfo -> RealtimeInfo));
        List<StatisticHistory> batchData = new ArrayList();
        for (VehicleInfo vehicleInfo : vehicleInfos) {
            StatisticHistory historyInfo = new StatisticHistory();
            RealtimeInfo realtimeInfo = realtimeInfoMap.get(vehicleInfo.getId());
            historyInfo.setVehicleId(vehicleInfo.getId());
            historyInfo.setBootTime(realtimeInfo.getBootTime());
            historyInfo.setDistance(realtimeInfo.getDistance());
            historyInfo.setElectricityConsume(realtimeInfo.getElectricityConsume());
            historyInfo.setFaultRatio(realtimeInfo.getFaultNumber()*100/150d);
            historyInfo.setOnlineState(realtimeInfo.getOnlineState());
            historyInfo.setCreateTime(new Date());
            historyInfo.setUpdateTime(new Date());
            batchData.add(historyInfo);
        }
        statisticHistoryDao.batchInsert(batchData);
        realtimeInfoDao.updateOnlineState(CterminalConst.OnlineState.ONLINE);
        log.info("数据统计任务结束...,跟新车辆：{}辆",batchData.size());
    }

}
